package com.pocketmenu.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;

public class PointOfInterestOverlay extends BalloonItemizedOverlay<OverlayItem> {

	private Context mContext;
	private ArrayList<OverlayItem> pois = new ArrayList<OverlayItem>();
	private Location currentLocation = ShowPointOfInterestMapActivity.currentLocation;
	
	public PointOfInterestOverlay(Drawable defaultMarker, MapView mapView) {
		super(defaultMarker,mapView);
	    boundCenter(defaultMarker);
	    mContext = mapView.getContext();
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return pois.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return pois.size();
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		String tmp = pois.get(index).getTitle();
	    GeoPoint itemPoint = pois.get(index).getPoint();
	    Location tmpLoc = convertGeoPointToLocation(itemPoint);
	    
	    double distance = ((currentLocation).distanceTo(tmpLoc));
	    DecimalFormat df = new DecimalFormat("#");
	    tmp = tmp + " a " + String.valueOf(df.format(distance)) + " metros";
	    
	    Toast.makeText(mContext,tmp,Toast.LENGTH_LONG).show();
	    return true;
	}
	
	public void addOverlay(OverlayItem overlay) {
	    pois.add(overlay);
	    populate();
	}
	
	public void setCurrentLocation(Location loc){
		this.currentLocation = loc;
	}
	
	public Location convertGeoPointToLocation(GeoPoint gp){
	    Location convertedLocation = new Location("");
	    convertedLocation.setLatitude(gp.getLatitudeE6() / 1e6);
	    convertedLocation.setLongitude(gp.getLongitudeE6() / 1e6);
	    return convertedLocation;
	}
	
	
}
